﻿using Newtonsoft.Json;
using Production.CommonLib.Constants;
using Production.CommonLib.Models;
using Swimj.CommonLib.Base;
using Swimj.Core.Extensions;

namespace Production.RazorLib.Services;

public class ProductionResourceService : IProductionResourceService
{
    private readonly IHttpClientFactory _httpClientFactory;

    public ProductionResourceService(IHttpClientFactory httpClientFactory)
    {
        _httpClientFactory = httpClientFactory;
    }
    
    public async Task<PaginatedResult<ProductionResourceInfoResponseDto>?> QueryProductionResourcesByPage(ProductionResourceSearchRequestDto model)
    {
        try
        {
            var httpClient = _httpClientFactory.CreateClient("productionApi");
            var response = await httpClient.GetAsync(model.ToQueryString(ApiRoutes.ProductionResource.QueryProductionResources));
            if (response.IsSuccessStatusCode)
            {
                var resultJson = await response.Content.ReadAsStringAsync();

                if (!string.IsNullOrEmpty(resultJson))
                {
                    return JsonConvert.DeserializeObject<PaginatedResult<ProductionResourceInfoResponseDto>>(resultJson);
                }
            }

            return null;
        }
        catch (Exception e)
        {
            Console.WriteLine(e);
            throw;
        }
    }

    public Task<Result<ProductionResourceInfoResponseDto>?> UpdateProductionResource(int key, ProductionResourceEditRequestDto editRequestDto)
    {
        throw new NotImplementedException();
    }

    public Task<Result<ProductionResourceInfoResponseDto>?> CreateProductionResource(ProductionResourceEditRequestDto editRequestDto)
    {
        throw new NotImplementedException();
    }

    public Task<Result<ProductionResourceInfoResponseDto>?> GetProductionResource(int key)
    {
        throw new NotImplementedException();
    }
}